/**
 * 
 */
package com.laozhao.EasySales.dialog;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

import com.laozhao.EasySales.model.AbstractModel;
import com.laozhao.EasySales.zgui.ZButton;
import com.laozhao.EasySales.zgui.ZDialog;
import com.laozhao.dreamframe.util.FrameFactory;

/**
 * @author dozhao
 * @version create time：Aug 2, 2011 11:24:13 AM
 * 
 */
/**
 * @author dozhao
 * 
 */
public abstract class AbstractDialog<E extends AbstractModel> extends ZDialog {

	protected ZButton saveButton = new ZButton("Save");
	protected ZButton keepButton = new ZButton("Save and Add");
	protected ZButton cancelButton = new ZButton("Cancel");

	protected E dataModel;

	private Action action = new Action();

	public void setModelData(E model) {
		this.dataModel = model;
	}

	public AbstractDialog() {
		super(FrameFactory.getMainFrame(), true);
		initEvent();
//		setResizable(false);
	}

	public abstract void init();

	private void initEvent() {
		cancelButton.addActionListener(action);
		saveButton.addActionListener(action);
		// this.addKeyListener(action);
	}

	protected void initComponents() {
//		setVisible(true);
		getContentPane().setLayout(new BorderLayout(40, 20));

		JPanel controlPanel = new JPanel(new GridLayout(5, 1, 5, 5));
		controlPanel.add(saveButton);
		controlPanel.add(keepButton);
		controlPanel.add(cancelButton);
		getContentPane().add(controlPanel, BorderLayout.EAST);
	}

	@Override
	protected JRootPane createRootPane() {
		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		JRootPane rootPane = new JRootPane();
		rootPane.registerKeyboardAction(action, stroke,
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		return rootPane;

	}

	class Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			setVisible(false);
//			dispose();
		}
	}
}
